package com.edward.service;


import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.Role;
import com.edward.model.User;


public class UserManager extends HibernateDaoSupport {

	public void addUser(User user) {
		
		Role r =(Role)getHibernateTemplate().get(Role.class, 1);
		user.getRoles().add(r);
		user.setStatus(1);
		getHibernateTemplate().save(user);
	}
	
	public void updateUser(User user) {
		
		getHibernateTemplate().update(user);
	}
	
	public void deleteUser(int id){
		
		getHibernateTemplate().delete(getUser(id));
	}
	
	public User getUser(int id){
		User d =(User)getHibernateTemplate().get(User.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<User> listUser() {
		return getHibernateTemplate().find("from User");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit) {
		List <User>list = getHibernateTemplate().find("from User" );
		for (User user : list) {
			user.setRoles(null);
		}
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <User>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	



}



